<?php
declare(strict_types=1);

namespace app\admin\controller;
use think\facade\Session;
use think\exception\ValidateException;
use think\facade\Db;
class Classify extends Base
{

    protected $mod;

    protected function initialize()
    {
        parent::initialize();
        $this->mod = new \app\admin\model\Classify;
    }
    public function index($pid = 0,$keyword='',$page=1,$limit=50)
    {
        $map=[];
        $map[]=['pid','=',$pid];
        if($keyword){
            $map[]=['title','like',"%$keyword%"];
        }
        $dataList = $this->mod->where($map)->paginate($limit, false, ['page' => $page, 'query' => ['keyword' => $keyword]]);
        return $this->success('ok', $dataList);
    }

    public function mselect($pid=0){
        $dataList=$this->mod->field('id,title,pid,havesid,path')->where('pid',$pid)->select();
        return $this->success('ok',$dataList);
    }


    public function read($id=0){
        $res=$this->mod::find($id);
        return $this->success('ok',$res);
    }

    public function getSelect($pid = 0, $ids = [])
    {
        Session::flash('classify','');



        if ($pid) {
            if ($res=$this->mod->getSelected($pid, $ids)) {
                return $this->success('获取分类成功', $res);
            } else {
                return $this->success('无子分类');
            }
        } else {
            return $this->error('未指定分类');
        }
    }
    public function getpathinfo(){
        $ids=input('ids',[]);
        if(empty($ids)){
            return [];
        }
        $pathinfo='/';

        $r=$this->mod::where('id','in',$ids)->column('title');
        if($r){
            $pathinfo=implode(',',$r);
        }
        return $pathinfo;
    }

    public function getPath($id=0){
        $path=db::name('classify')->where('id',$id)->value('path');
        $path=$path.$id;

        $r=$this->mod::where('id','in',$path)->field('id,title,pid')->select();

        return $this->success('ok',$r);


    }


    public function getTree($id)
    {
        $dataList = $this->mod::order('sort', 'asc')->whereRaw('id !='.$id.' and not find_in_set('.$id.',path)')->select()->toArray();
        $ruleList = generateTree($dataList);
        return $this->success('获取成功', $ruleList);
    }

    public function getchild($id){
        $r=db::name('classify')->where('category_id',$id)->find();
      
        if(!$r){
            return $this->success('获取成功',[]);exit;
        }
        $cid=$r['id'];
       
        $dataList=$this->mod::whereRaw('find_in_set('.$cid.',path)')->select()->toArray();

        $ruleList = generateTree($dataList,$cid);
        return $this->success('获取成功',$ruleList);
    }


    public function save()
    {
        if (input("?post.title")) {
            $titles = input('post.title');
            $pid = input('post.pid/d');
            $data=[];
            foreach (explode("\n", $titles) as $key => $value) {
                $arr = explode('|', $value);
                if (empty(trim($arr[0]))) {
                    continue;
                }

                if($this->mod::where('title',trim($arr[0]))->find()){
                    continue;
                }
                $data[$key]['title'] = trim($arr[0]);
                $data[$key]['sort'] = isset($arr[1]) ? (int) $arr[1] : 0;
                $data[$key]['pid'] =  $pid;
            }
            if(empty($data)){

                return $this->error('增加失败'); 
            }
            if ($this->mod->saveAll($data)) {
                return $this->success('增加成功');
            } else {
                return $this->error('增加失败'); 
            }
        } else {

            return $this->error('请输入分类名称'); 
        }
    }
    public function update()
    {
        $data = $this->request->param();
        $data['pid']=input('pid',0);
        if (empty($data['title'])) {
            return $this->error('请输入标题'); 
        }
        if ($this->mod::update($data)) {

            return $this->success('更新成功');
        } else {
            return $this->error('更新失败'); 
        }
    }
    public function delete($ids)
    {
        $data = $this->mod->select();
        if (is_array($ids)) {
            $ids = array_map('intval', $ids);
            if (count($ids) == 1) {
                $ids = (int) $ids[0];
            }
        }
        if ($this->mod->destroy($ids)) {
            return $this->success('删除成功');
        } else {
            return $this->error('删除失败');           
        }
    }
}
